
package ch.hearc.coursJava.examen.dessin;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.rmi.RemoteException;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import ch.hearc.coursJava.exa.swing.MainWindow;

import com.bilat.tools.reseau.rmi.RmiTools;

/**
 *
 * @author raphael.capocasa
 *
 */
public class JFrameDessin extends JFrame implements Dessin_I
    {
    
    /**
     * 
     */
    private static final long serialVersionUID = 6036243000475607736L;
    
    /*------------------------------------------------------------------*\
    |*							Constructeurs							*|
    \*------------------------------------------------------------------*/
    public JFrameDessin()
        {
        shareRMI();
        creerItemMenu();
        creerMenu();
        
        this.setLocation(0, 0);
        this.setTitle("Animation d'un cercle");
        this.setMinimumSize(new Dimension(800, 600));
        ImageIcon imageIcon = new ImageIcon("./img/rose.png");
        this.image = imageIcon.getImage();
        this.setIconImage(image);
        
        creerPanels();
        placerPanels();
        creerToolbar();
        addListener();
        
        readSettings();
        
        thread = new Thread(panelCentre);
        thread.start();
        
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        this.setVisible(true); //Par d�faut: NON-visible, A mettre en dernier
        }
    
    private void shareRMI()
        {
        try
            {
            RmiTools.shareObject(this, RmiTools.getPortDefaultRMI(), JFrameDessin.ID);
            }
        catch (RemoteException e)
            {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
    
    /*------------------------------------------------------------------*\
    |*							Methodes Public							*|
    \*------------------------------------------------------------------*/
    @Override
    public MyThread giveThread() throws RemoteException
        {
        return new MyThread(thread);
        }
    
    /*------------------------------------------------------------------*\
    |*							Methodes Private						*|
    \*------------------------------------------------------------------*/
    
    private void placerPanels()
        {
        BorderLayout layout = new BorderLayout();
        setLayout(layout);
        add(panelCentre, BorderLayout.CENTER);
        }
    
    private void creerPanels()
        {
        panelCentre = new JPanelCentre(this);
        }
    
    private void creerItemMenu()
        {
        this.itemQuitter = new JMenuItem("Quitter");
        this.itemQuitter.setMnemonic(KeyEvent.VK_Q);
        itemQuitter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        
        this.itemBleu = new JMenuItem("Bleu");
        itemBleu.setMnemonic(KeyEvent.VK_B);
        itemBleu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.ALT_MASK));
        
        this.itemRouge = new JMenuItem("Rouge");
        itemRouge.setMnemonic(KeyEvent.VK_R);
        itemRouge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK));
        
        this.itemPlus = new JMenuItem("+");
        itemPlus.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK));
        
        this.itemMoins = new JMenuItem("-");
        itemMoins.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK));
        
        this.itemApropos = new JMenuItem("A propos !");
        }
    
    private void creerMenu()
        {
        this.menuBar = new JMenuBar();
        
        this.menuFichier = new JMenu("Fichier");
        menuFichier.setMnemonic(KeyEvent.VK_F);
        this.menuBar.add(menuFichier);
        
        this.menuEdition = new JMenu("Edition");
        menuEdition.setMnemonic(KeyEvent.VK_E);
        
        this.menuCouleurs = new JMenu("Couleurs");
        menuCouleurs.setMnemonic(KeyEvent.VK_C);
        
        this.menuVitesse = new JMenu("Vitesse");
        menuVitesse.setMnemonic(KeyEvent.VK_V);
        
        this.menuAide = new JMenu("Aide");
        menuAide.setMnemonic(KeyEvent.VK_A);
        this.menuAide.add(itemApropos);
        
        this.menuVitesse.add(itemPlus);
        this.menuVitesse.add(itemMoins);
        this.menuCouleurs.add(itemBleu);
        this.menuCouleurs.add(itemRouge);
        this.menuEdition.add(menuCouleurs);
        this.menuEdition.add(menuVitesse);
        this.menuBar.add(menuEdition);
        this.menuBar.add(menuAide);
        
        this.menuFichier.addSeparator();
        this.menuFichier.add(itemQuitter);
        
        this.setJMenuBar(menuBar);
        }
    
    private void creerToolbar()
        {
        this.toolBar = new JToolBar(SwingConstants.HORIZONTAL);
        this.toolBar.setFloatable(true);
        
        this.toolQuitter = new JButton(new ImageIcon("./img/close.png"));
        this.toolBar.add(toolQuitter);
        
        this.toolPlus = new JButton(new ImageIcon("./img/plus.png"));
        this.toolMoins = new JButton(new ImageIcon("./img/moins.png"));
        
        this.toolBar.add(toolPlus);
        this.toolBar.add(toolMoins);
        
        this.getContentPane().add(toolBar, BorderLayout.NORTH);
        }
    
    private void writeSettings()
        {
        Properties properties = new Properties();
        properties.setProperty("positionX", new Integer(getX()).toString());
        properties.setProperty("positionY", new Integer(getY()).toString());
        properties.setProperty("width", new Integer(getWidth()).toString());
        properties.setProperty("height", new Integer(getHeight()).toString());
        
        try
            {
            PROPERTIES_FILE.getParentFile().mkdirs();
            FileOutputStream fileOutputStream = new FileOutputStream(PROPERTIES_FILE);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            properties.store(bufferedOutputStream, MainWindow.class.getSimpleName());
            
            bufferedOutputStream.close();
            fileOutputStream.close();
            }
        catch (Exception e)
            {
            System.err.println("Impossible d'enregistrer les propri�t�s du programme !\n" + e.toString());
            }
        }
    
    private void readSettings()
        {
        Properties properties = new Properties();
        try
            {
            FileInputStream fileInputStream = new FileInputStream(PROPERTIES_FILE);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
            properties.load(bufferedInputStream);
            int x = Integer.valueOf(properties.getProperty("positionX", "5"));
            int y = Integer.valueOf(properties.getProperty("positionY", "20"));
            int h = Integer.valueOf(properties.getProperty("height", "600"));
            int w = Integer.valueOf(properties.getProperty("width", "800"));
            
            setLocation(x, y);
            setSize(w, h);
            
            bufferedInputStream.close();
            fileInputStream.close();
            }
        catch (Exception e)
            {
            System.err.println("Impossible de lire les propri�t�s !\n" + e.toString());
            }
        }
    
    private void addListener()
        {
        itemQuitter.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent arg0)
                    {
                    processWindowEvent(new WindowEvent(JFrameDessin.this, WindowEvent.WINDOW_CLOSING));
                    }
            });
        
        this.addWindowListener(new WindowAdapter()
            {
                
                @Override
                public void windowClosing(WindowEvent e)
                    {
                    int confirmation = JOptionPane.showConfirmDialog(null, "Voulez-vous vraiment quitter ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    
                    if (confirmation == JOptionPane.YES_OPTION)
                        {
                        writeSettings();
                        System.exit(NORMAL);
                        }
                    }
            });
        
        this.toolQuitter.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent arg0)
                    {
                    itemQuitter.doClick();
                    }
            });
        
        this.itemRouge.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent arg0)
                    {
                    panelCentre.setColorCercle(Color.RED);
                    }
            });
        
        this.itemBleu.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent arg0)
                    {
                    panelCentre.setColorCercle(Color.BLUE);
                    }
            });
        
        this.itemPlus.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                    {
                    panelCentre.plusAlpha();
                    }
            });
        
        this.itemMoins.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                    {
                    panelCentre.moinsAlpha();
                    }
            });
        
        this.toolPlus.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent arg0)
                    {
                    itemPlus.doClick();
                    }
            });
        
        this.toolMoins.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent arg0)
                    {
                    itemMoins.doClick();
                    }
            });
        
        this.itemApropos.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                    {
                    JOptionPane.showMessageDialog(null, "Examen de Java\nPar Rapha�l Capocasale", "A propos !", JOptionPane.INFORMATION_MESSAGE);
                    }
            });
        }
    
    /*------------------------------------------------------------------*\
    |*							Attributs Private						*|
    \*------------------------------------------------------------------*/
    //tools
    private Image image;
    private Thread thread;
    
    private JPanelCentre panelCentre;
    
    //tools menu
    private JMenuBar menuBar;
    
    private JMenu menuFichier;
    private JMenu menuEdition;
    private JMenu menuCouleurs;
    private JMenu menuVitesse;
    private JMenu menuAide;
    
    private JMenuItem itemQuitter;
    private JMenuItem itemBleu;
    private JMenuItem itemRouge;
    private JMenuItem itemPlus;
    private JMenuItem itemMoins;
    private JMenuItem itemApropos;
    
    //tools toolBar
    private JToolBar toolBar;
    private JButton toolQuitter;
    private JButton toolPlus;
    private JButton toolMoins;
    
    /*------------------------------*\
    |*			  Static			*|
    \*------------------------------*/
    private static final File PROPERTIES_FILE = new File("config" + File.separator + MainWindow.class.getSimpleName());
    public static final String ID = "idDessin";
    }
